import { NextFunction, Request, Application, Response } from "express";
const express = require('express');
const app: Application = express();
// 路由模块
const homeRoute = require('./route/home');
const userRoute = require('./route/user');
// 处理post的参数
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
// 配置模板引擎
const path = require("path");
app.engine("art", require("express-art-template"));
console.log('__dirname:', __dirname);
app.set("views", path.join(__dirname, "view"));
app.set("view engine", "art");

app.use((req: Request, res: Response, next: NextFunction) => {
  console.log('前置之间件>>>');
  // res.send("系统维护中");
  next();
});
// 静态资源
app.use(express.static('public'));

// app.get('/index', (req: Request, res: Response) => {
//   // send 方法以及完成了对响应头类型的设置、状态吗和编码
//   res.send('index');
// });

app.use('/users', userRoute);
app.use('/home', homeRoute);

app.get('/err', (req: Request, res: Response) => {
  throw new Error('this is err...');
});

// 404接口的处理
app.use((req: Request, res: Response, next: NextFunction) => {
  // res.sendStatus(404);
  res.send(404, '404错误页面');
});
// 统一处理错误中间件
app.use((err: any, req: Request, res: Response) => {
  res.status(500).send(err);
});

app.listen(3003, () => {
  console.log('server start port is 3003...');
});
